﻿using System.Diagnostics;
using System.Reflection;
using NUnit.Framework;

namespace LightFactory.Extensions.Tests
{
    public class LightFactoryExtensionsTests
    {
        [Test]
        public void CreationWithMetaBaseFactory()
        {
            Assert.True(true);
            //Assert.True(false);
            Stopwatch watch = Stopwatch.StartNew();
            FactoryBase.RegisterFactory(new MetaBasedFactory());
            var elapsed = watch.ElapsedMilliseconds;
            watch.Reset();
            Assert.True(elapsed < 200, "Factory registration executed by " + elapsed + " ms... Seams like performance is too low! If it's first test run, run test once more.");

            watch.Start();
            FactoryBase.RegisterFactory(new MetaBasedFactory(Assembly.GetExecutingAssembly()));
            elapsed = watch.ElapsedMilliseconds;
            watch.Stop();
            Assert.True(elapsed < 200, "Factory registration executed by " + elapsed + " ms... Seams like performance is too low! If it's first test run, run test once more.");

            Assert.AreEqual("Test", Factory<ITestObject>.Create("Test").GetMessage());
            Assert.AreEqual(Test.TEST_O, Factory<ITestObject>.Create().GetMessage());
        }

        [Test]
        public void MetaBasedFactoryObjectsCreationPerformanceTest()
        {
            FactoryBase.RegisterFactory(new MetaBasedFactory());

            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < 10000; i++)
			{
                Factory<ITestObject>.Create("Test").GetMessage();
			}
            var elapsed = watch.ElapsedMilliseconds;
            watch.Stop();
            Assert.True(elapsed < 30, "10000 method calls executed by " + elapsed + " ms... Seams like performance is too low! If it's first test run, run test once more.");
        }
    }
}